package com.kaifamiao.exer;

import java.io.PipedReader;

/**
 * copy 对象
 *    Object::clone 拷贝对象
 *    默认是 浅拷贝 的实现
 */
public class Exercise02 {

    public static void main(String[] args) throws CloneNotSupportedException {
        InnerField innerField = new InnerField();

        Top top = new Top();
        top.top1 = 1;
        top.innerField = innerField;
        top.innerField.f = 10;

        // 拷贝对象
        Top copy = top.clone();

        System.out.println("top: " + top);
        System.out.println("copy: " + copy);

        System.out.println("top.innerField: " + top.innerField);
        System.out.println("copy.innerField: " + copy.innerField);

        top.innerField.f = 20;

        System.out.println("top.innerField: " + top.innerField.f);
        System.out.println("copy.innerField: " + copy.innerField.f);
//        copy.top();
    }
}



class Top implements Cloneable{
    public int top1;

    // 引用数据类型
    public InnerField innerField;

    public void top(){
        System.out.println("top");
    }

    @Override
    protected Top clone() throws CloneNotSupportedException {
//        Top copy = (Top) super.clone();
//        // 深 copy
//        // 引用数据类型字段的 copy
//        InnerField innerField = copy.innerField.clone();
//        copy.innerField = innerField;
//
//        return copy;

        /*
            使用 json 实现深 copy
              对象 <--> json 串
         */


        return null;
    }
}

class InnerField implements Cloneable{

    public int f;

    public A a;

    @Override
    protected InnerField clone() throws CloneNotSupportedException {
        return (InnerField) super.clone();
    }
}

class A{
    public B b;
}

class B{

}
